package com.sheliduan.itlearning.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.common.domain.dos.CategoryDO;
import com.sheliduan.itlearning.common.domain.dos.TagDO;
import com.sheliduan.itlearning.common.domain.dos.UserDO;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: 断舍离
 * @date: 2025/2/10 10:12
 * @description: 分类映射接口
 **/
public interface CategoryMapper extends BaseMapper<CategoryDO> {

    /*
     * 根据查询条件分页查询分类数据
     * */
    default Page<CategoryDO> selectPageList(long current, long size, String name, LocalDateTime startDate, LocalDateTime endDate){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<CategoryDO> page = new Page<>(current, size);
        LambdaQueryWrapper<CategoryDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), CategoryDO::getName, name)
                .ge(Objects.nonNull(startDate), CategoryDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), CategoryDO::getCreateTime, endDate) // 小于等于 endDate
                .orderByDesc(CategoryDO::getCreateTime);
        return selectPage(page, wrapper);
    }

    /*
     * 查询全部分类, 选择课程分类时调用
     * */
    default List<CategoryDO> selectAll() {
        return selectList(null);
    }

    /*
     * 根据分类id修改分类信息(管理员用的)
     * */
    default int updateCategory(Integer id, String name){
        LambdaUpdateWrapper<CategoryDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CategoryDO::getName, name);
        wrapper.set(CategoryDO::getUpdateTime, new Date());
        wrapper.eq(CategoryDO::getId, id);
        return update(null, wrapper);
    }
}
